using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour {
	
	private static int PLAYER_COUNT = 0;
	
	public Transform _prefabMoneyHUD;
	public Transform _prefabListHUD;
	public ItemManager _itemManager;  //manger for the items -set at instantiation
	public int _amountOfItemsInList; // Amount of items the player has in his shopping list
	
	public Transform guyObject;
	public float movementSpeed;
	public float turningSpeed;
	public Vector3 moving;
	
	private MoneyScript _money;
	private ListScipt _itemList;
	private float thisHorizontal;
	private float vertical;
	private bool shopping; //These need to be changed
	private Shop playerShop; //These need to changed
	
	public bool _moveUp;
	public bool _moveLeft;
	public bool _moveRight;
	public bool _moveJump;
	public bool _moveSpace;
	public ControlSceme _sceme;
	
	public ControlSceme ControlScheme {
		get{
			return _sceme;	
		}
	}
	
	
	private bool touchingPlatform;
	
	
	//private Dictionary<Item, bool> _shoppingList; // List of all the items that the player needs to buy
	
	public double Money{
		get {
			return _money.Money;
		}
		
		set {
			_money.Money = value;
		}
	}
	
	public ListScipt ShoppingList {
		get {
			return _itemList;
		}
	}
	
	public bool Shopping {
		get{
			return shopping;
		}
	}
	
		
	// Use this for initialization
	void Start () {
		Transform moneyHUD = (Transform) Instantiate(_prefabMoneyHUD);
		MainDisplay moneyDisplay = moneyHUD.GetComponent<MainDisplay>();
		moneyDisplay._bounds.x += PLAYER_COUNT * 220;
		_money = moneyHUD.GetComponent<MoneyScript>();
		Money = 200.0;
		
		Transform listHUD = (Transform) Instantiate (_prefabListHUD);
		MainDisplay listDisplay = listHUD.GetComponent<MainDisplay>();
		listDisplay._bounds.x += PLAYER_COUNT * 220;
		_itemList = listHUD.GetComponent<ListScipt>();
		_itemList.BootShrap(_itemManager.GetShoppingList(_amountOfItemsInList));
		
		moving = new Vector3(1f,0f,0f);
		thisHorizontal = 0f;
		vertical = 0f;
		shopping = false;
		
		//playerKeys[0] = new KeyCode();
		//playerKeys[1] = new KeyCode();
		//playerKeys[2] = new KeyCode();
		
		
		//_shoppingList = _itemManager.GetShoppingList(_amountOfItemsInList);
		PLAYER_COUNT++;
	}
	
	// Update is called once per frame

	
    void Update() {
		/*if (shopping == false)
		{
        //float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
		
		//rigidbody.MoveRotation(Quaternion.AngleAxis(thisHorizontal, new Vector3(0f,1f,0f)));
		//rigidbody.AddForce(moving,ForceMode.Force);
				
		//if (rigidbody.velocity.magnitude > 2)rigidbody.AddForce(-moving*3,ForceMode.Force);
				
			
		}*/
		
		
		
		
		
		if (shopping == true)
		{
			transform.localPosition = playerShop.transform.localPosition;
			rigidbody.isKinematic = true;
			
			guyObject.animation.Stop();				
		
			
			//thisHorizontal = 
			
			//have to stop animation here - causing problems
			//guyObject.animation.Stop(); //why causing problems?
			
						
			if(_moveSpace)
			{
				//moving = new Vector3(1f* movementSpeed * Time.deltaTime,0f,0f);
				//moving = Quaternion.AngleAxis(thisHorizontal, Vector3.up)*moving;
				//moving += playerShop.transform.localPosition + 
				
				_moveSpace = false;
				shopping = false;
				Quaternion backwards = playerShop.transform.localRotation;
				Vector3 backwardsHalf = new Vector3(0f,0f,-1.0f);
				backwardsHalf = backwards * backwardsHalf;
				rigidbody.transform.localPosition += backwardsHalf;
				rigidbody.isKinematic = false;
				rigidbody.AddForce(backwardsHalf*50,ForceMode.Force);
				
				thisHorizontal = Quaternion.Angle(playerShop.transform.localRotation, Quaternion.identity);
				thisHorizontal += 90;
				if (backwardsHalf.x>0)thisHorizontal +=180;
				
				
				
				
				guyObject.animation.Play();
			
				
				//rigidbody.AddForce(
			}
			
		}
			
		
	}
	
	void FixedUpdate ()
	{
		if (shopping == false)
		{
			if(vertical >0)vertical = 0;
			
			if (_moveRight) {
				thisHorizontal +=1.5f* turningSpeed * Time.deltaTime;
				_moveRight = false;
			}
			if (_moveLeft) {
				thisHorizontal -=1.5f* turningSpeed * Time.deltaTime;
								_moveLeft = false;
			}
			if (_moveUp) {
				vertical = 0.9f * movementSpeed * Time.deltaTime;
								_moveUp = false;
			}
			
			if (transform.localPosition.y<0.4551f) touchingPlatform = true;//bodge for ground sticking off stairs
			
			if (_moveJump && touchingPlatform) 
			{
				rigidbody.AddForce(new Vector3(0f,60f,0f),ForceMode.Force);
				_moveJump = false;
				
			}
			
			
				
        	moving = new Vector3(vertical,0f,0f);
			moving = Quaternion.AngleAxis(thisHorizontal, Vector3.up) *moving;
			
			
			rigidbody.MoveRotation(Quaternion.AngleAxis(thisHorizontal, new Vector3(0f,1f,0f)));
			rigidbody.AddForce(moving,ForceMode.Force);
			
			//if (!(guyObject.animation.isPlaying)) //this is always true even after stop
			guyObject.animation.Play();//fix flickering
			
			if (rigidbody.velocity.magnitude < 0.05f) guyObject.animation.Stop();//stop/start the walk animation
			
			
			
			foreach (AnimationState state in guyObject.animation) 
			{
				
 		   		state.speed = 25f * rigidbody.velocity.magnitude*2f * Time.deltaTime;
			}
			
			if (rigidbody.velocity.magnitude > 3) rigidbody.AddForce(-moving,ForceMode.Force);
		}
	}
	
	public void inShop(Shop inShop)
	{
		shopping = true;
		playerShop = inShop;
	}
	
	public void SetItemMan(ItemManager itemMan)
	{
		_itemManager = itemMan;
	}
	/*
	public void SetKeys(KeyCode Up, KeyCode Left, KeyCode Right)
	{
		playerKeys[0] = Up;
		playerKeys[1] = Left;
		playerKeys[2] = Right;
	}*/
	//can't use keycode in this way as they are listeners.
		
	
	public void changeColor()
	{
		
	}
	
	public void Up() {
		_moveUp = true;
	}
	
	public void Down() {
		
	}
	
	public void Left() {
		_moveLeft = true;
	}
	
	public void Right() {
		_moveRight = true;
	}
	
	public void Jump() {
		
		if(touchingPlatform) _moveJump = true;
	}
	
	public void Space() {
		if(shopping) _moveSpace = true;
	}
	
	
	void OnCollisionEnter ()
	{
		touchingPlatform = true;
	}
	
	void OnCollisionExit ()
	{
		touchingPlatform = false;
	}
}
